<?php
/**
 * NetBiz Framework
 * Copyright (C) 20009 - 2010 NetBiz
 * 
 * All rights reserved.
 */
 
require_once 'Smarty/libs/Smarty.class.php';

class FRM_Smarty extends Smarty {

	/**
	 * Overriding of smarty fetch function to automatically load if necessary
	 * a language file which have the same name as the tpl (followed by .ini)
	 *
	 * Load as well the common.ini tree, and prepare 3 config vars scope :
	 *   - specific
	 *   - merged
	 *   - local
	 * in that order on the stack
	 */
	function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false)
	{
		if (FRM_Config::get('I18N','SMARTY_LANGUAGE_RESOURCE'))
		{
			// Load automatically the ressource file with the same name
			$this->FRM_config_load($resource_name);

			// Save this specific context in the stack
			array_unshift($this->_config,$this->_config[0]);

			// Load common tree config for the resource
			$this->load_common_config_tree($resource_name);

			// Merge back the specific context in the stack (merged context)
			$this->_config[0]['vars'] = @array_merge($this->_config[0]['vars'],$this->_config[1]['vars']);
			$this->_config[0]['files'] = @array_merge($this->_config[0]['files'],$this->_config[1]['files']);

			// Align to smarty_include behaviour and save a slot for tpl local space
			array_unshift($this->_config,$this->_config[0]);
		}

		return parent::fetch($resource_name,$cache_id,$compile_id,$display);
	}

	/**
	 * Overriding of smarty include function to automatically load if necessary
	 * a language file which have the same name as the tpl (followed by .ini)
	 *
	 * Load as well the common.ini tree, and prepare 3 config vars scope :
	 *   - specific
	 *   - merged
	 *   - local
	 * in that order on the stack
	 */
	function _smarty_include($params)
	{
		if (FRM_Config::get('I18N','SMARTY_LANGUAGE_RESOURCE'))
		{
			// Save the merged context, and reload the specific context
			// of the previous script
			array_unshift($this->_config, $this->_config[2]);

			// Load the specific context of the included script
			$this->FRM_config_load($params['smarty_include_tpl_file']);

			// Save this specific context in the stack
			array_unshift($this->_config,$this->_config[0]);

			// Load common tree config for the resource
			$this->load_common_config_tree($params['smarty_include_tpl_file']);

			// Merge back the specific context in the stack (merged context)
			$this->_config[0]['vars'] = @array_merge($this->_config[0]['vars'],$this->_config[1]['vars']);
			$this->_config[0]['files'] = @array_merge($this->_config[0]['files'],$this->_config[1]['files']);

			parent::_smarty_include($params);

			// Get rid of the merged context
			array_shift($this->_config);
			// Get rid of the specific context
			array_shift($this->_config);
		}
		else
		{
			parent::_smarty_include($params);
		}
	}

	/**
	 * Load the correct section of an ini multilanguage file
	 *
	 * @param string the name of the resource to load, without the ".ini" extension
	 * @return none
	 */
	function FRM_config_load($resource_name)
	{
		if ( $this->_parse_resource_name(
			$prnp = array('resource_name' => $resource_name.".ini",
				'resource_base_path' => $this->config_dir,
				'get_source' => false)) )
		{
			$this->config_load($resource_name.".ini",FRM_MultiLanguage::getLanguage(),'local');
		}
	}

	/**
	 * Works only with file resources, inside the template_dir
	 * So, resource_name has for constraints to be a relative path
	 * like 'user/form/edit.tpl'
	 * and can have the optionnal 'file' resource indicator, like
	 * 'file:user/form/edit.tpl'
	 *
	 * Absolute file path are ignored.
	 *
	 * @param string the name of the resource for which to load
	 *   the common ini files tree
	 * @return bool FALSE if the resource_name is invalid,
	 *   TRUE otherwise
	 */
	function load_common_config_tree($resource_name)
	{
		// split tpl_path by the first colon
		$_resource_name_parts = explode(':', $resource_name, 2);
	
		if (count($_resource_name_parts) == 1)
		{
			// no resource type given
			$resource_type = $this->default_resource_type;
			$resource_name = $_resource_name_parts[0];
		}
		else
		{
			if(strlen($_resource_name_parts[0]) == 1)
			{
				// 1 char is not resource type, but part of filepath
				$resource_type = $this->default_resource_type;
			}
			else
			{
				$resource_type = $_resource_name_parts[0];
				$resource_name = $_resource_name_parts[1];
			}
		}

		// Only file resources, and in the config dir
		if ($resource_type != 'file' or !is_file($this->config_dir . DIRECTORY_SEPARATOR . $resource_name))
		{
			return FALSE;
		}
		else
		{
			// split the resource name with DIR_SEP
			$path_array = explode(DIRECTORY_SEPARATOR, $resource_name);
			$cur_path = '';

			foreach($path_array as $path_part)
			{
				$this->FRM_config_load($cur_path . 'common');
				$cur_path .= $path_part . DIRECTORY_SEPARATOR;
			}

			return TRUE;
		}
	}
}
?>